#!/bin/bash
################################################################################
# File Name: auto_install_mongodb.sh
# Version: V1.0
# Author: Brian Hsiung
# Created Time : 2021-05-16 20:17:23
# Description:  MongoDB Cluster集群
###############################################################################

# 应用版本，根据实际情况修改
VERSION='4.0.3'

# 应用端口，可自定义，auto_init_cluster.sh上的端口需要这里保持一致
MONGOS_PORT=20000
CONFIG_SERVER_PORT=21000
SHARD_SERVER1_PORT=27001
SHARD_SERVER2_PORT=27002
SHARD_SERVER3_PORT=27003

# cacheSizeGB
CACHE_SIZE=0.5

# 集群IP列表，根据实际情况修改，auto_init_cluster.sh上的IP需要与这里一样
IPS=(192.168.157.131 192.168.157.132 192.168.157.133)

# 应用目录
MONGODB_CLUSTER_HOME=/opt/mongodb_cluster
CONF_HOME=$MONGODB_CLUSTER_HOME/conf
MONGOS_LOG_HOME=$MONGODB_CLUSTER_HOME/mongos/logs
CONFIG_DATA_HOME=$MONGODB_CLUSTER_HOME/config_server/data
CONFIG_LOG_HOME=$MONGODB_CLUSTER_HOME/config_server/logs
SHARD_SERVER1_DATA_HOME=$MONGODB_CLUSTER_HOME/shard_server1/data
SHARD_SERVER1_LOG_HOME=$MONGODB_CLUSTER_HOME/shard_server1/logs
SHARD_SERVER2_DATA_HOME=$MONGODB_CLUSTER_HOME/shard_server2/data
SHARD_SERVER2_LOG_HOME=$MONGODB_CLUSTER_HOME/shard_server2/logs
SHARD_SERVER3_DATA_HOME=$MONGODB_CLUSTER_HOME/shard_server3/data
SHARD_SERVER3_LOG_HOME=$MONGODB_CLUSTER_HOME/shard_server3/logs

function wait_time() {
    echo -e "==================== 等待5s ====================\n"
    for i in `seq 5`
    do
        echo -n '...'
        sleep 1
    done
    echo ""
}

function init_environment() {
    echo never > /sys/kernel/mm/transparent_hugepage/enabled
    echo never > /sys/kernel/mm/transparent_hugepage/defrag
    grep '/sys/kernel/mm/transparent_hugepage' /etc/rc.d/rc.local >/dev/null 2>&1
    if [ $? -ne 0 ]; then
cat >> /etc/rc.d/rc.local << EOF
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
    echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
    echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
EOF
    fi
    chmod +x /etc/rc.d/rc.local

    firewall-cmd --zone=public --add-port=$MONGOS_PORT/tcp --permanent >/dev/null 2>&1
    firewall-cmd --zone=public --add-port=$CONFIG_SERVER_PORT/tcp --permanent >/dev/null 2>&1
    firewall-cmd --zone=public --add-port=$SHARD_SERVER1_PORT/tcp --permanent >/dev/null 2>&1
    firewall-cmd --zone=public --add-port=$SHARD_SERVER2_PORT/tcp --permanent >/dev/null 2>&1
    firewall-cmd --zone=public --add-port=$SHARD_SERVER3_PORT/tcp --permanent >/dev/null 2>&1
    firewall-cmd --reload >/dev/null 2>&1
}

function install_mongodb_cluster() {
    grep 'mongodb' /etc/passwd >/dev/null 2>&1
    [ $? -eq 0 ] || useradd mongodb
    grep 'mongodb' /etc/group >/dev/null 2>&1
    [ $? -eq 0 ] || groupadd mongodb

    [ -d $MONGODB_CLUSTER_HOME ] || mkdir -p $MONGODB_CLUSTER_HOME
    [ -d $CONF_HOME ] || mkdir -p $CONF_HOME
    [ -d $MONGOS_LOG_HOME ] || mkdir -p $MONGOS_LOG_HOME
    [ -d $CONFIG_DATA_HOME ] || mkdir -p $CONFIG_DATA_HOME
    [ -d $CONFIG_LOG_HOME ] || mkdir -p $CONFIG_LOG_HOME
    [ -d $SHARD_SERVER1_DATA_HOME ] || mkdir -p $SHARD_SERVER1_DATA_HOME
    [ -d $SHARD_SERVER1_LOG_HOME ] || mkdir -p $SHARD_SERVER1_LOG_HOME
    [ -d $SHARD_SERVER2_DATA_HOME ] || mkdir -p $SHARD_SERVER2_DATA_HOME
    [ -d $SHARD_SERVER2_LOG_HOME ] || mkdir -p $SHARD_SERVER2_LOG_HOME
    [ -d $SHARD_SERVER3_DATA_HOME ] || mkdir -p $SHARD_SERVER3_DATA_HOME
    [ -d $SHARD_SERVER3_LOG_HOME ] || mkdir -p $SHARD_SERVER3_LOG_HOME

    cp mongodb-linux*.tgz $MONGODB_CLUSTER_HOME
    cd $MONGODB_CLUSTER_HOME
    tar -zxf mongodb-linux*-${VERSION}.tgz 2>/dev/null
    rm -f mongodb-linux*-${VERSION}.tgz
    if [ -d $MONGODB_CLUSTER_HOME/mongodb-$VERSION ]; then
        rm -rf $MONGODB_CLUSTER_HOME/mongodb-$VERSION
    fi
    mv mongodb-linux* mongodb-$VERSION

    grep 'MONGODB_HOME' /etc/profile >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "MONGODB_HOME=$MONGODB_CLUSTER_HOME/mongodb-$VERSION" >> /etc/profile
        echo "PATH=\$PATH:\$MONGODB_HOME/bin" >> /etc/profile
    fi
    source /etc/profile

# config.conf文件
cat > $CONF_HOME/config.conf << EOF
systemLog:
  destination: file
  logAppend: true 
  path: $CONFIG_LOG_HOME/configsvr.log
storage:
  dbPath: $CONFIG_DATA_HOME
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: $CONFIG_LOG_HOME/configsvr.pid 
net:
  port: $CONFIG_SERVER_PORT
  bindIp: 0.0.0.0
replication:
  replSetName: configs
sharding:
  clusterRole: configsvr
EOF
# shard_server1.conf(三个shard配置文件可以写成函数:log和pid的前缀与conf的前缀一致，但这样还需要修改文档，想了想还是算了)
cat > $CONF_HOME/shard_server1.conf << EOF
systemLog:
  destination: file
  logAppend: true
  path: $SHARD_SERVER1_LOG_HOME/shard1.log
storage:
  dbPath: $SHARD_SERVER1_DATA_HOME
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: $CACHE_SIZE
processManagement:
  fork: true
  pidFilePath: $SHARD_SERVER1_LOG_HOME/shard1.pid
net:
  port: $SHARD_SERVER1_PORT
  bindIp: 0.0.0.0
replication:
  replSetName: shard_server1
sharding:
  clusterRole: shardsvr
EOF
# shard_server2.conf
cat > $CONF_HOME/shard_server2.conf << EOF
systemLog:
  destination: file
  logAppend: true
  path: $SHARD_SERVER2_LOG_HOME/shard2.log
storage:
  dbPath: $SHARD_SERVER2_DATA_HOME
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: $CACHE_SIZE
processManagement:
  fork: true
  pidFilePath: $SHARD_SERVER2_LOG_HOME/shard2.pid
net:
  port: $SHARD_SERVER2_PORT
  bindIp: 0.0.0.0
replication:
  replSetName: shard_server2
sharding:
  clusterRole: shardsvr
EOF
# shard_server3.conf
cat > $CONF_HOME/shard_server3.conf << EOF
systemLog:
  destination: file
  logAppend: true
  path: $SHARD_SERVER3_LOG_HOME/shard3.log
storage:
  dbPath: $SHARD_SERVER3_DATA_HOME
  journal:
    enabled: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: $CACHE_SIZE
processManagement:
  fork: true
  pidFilePath: $SHARD_SERVER3_LOG_HOME/shard3.pid
net:
  port: $SHARD_SERVER3_PORT
  bindIp: 0.0.0.0
replication:
  replSetName: shard_server3
sharding:
  clusterRole: shardsvr
EOF
# mongos.conf
cat > $CONF_HOME/mongos.conf << EOF
systemLog:
  destination: file
  logAppend: true
  path: $MONGOS_LOG_HOME/mongos.log
processManagement:
  fork: true
  pidFilePath: $MONGOS_LOG_HOME/mongos.pid
net:
  port: $MONGOS_PORT
  bindIp: 0.0.0.0
sharding:
  configDB: configs/${IPS[0]}:$CONFIG_SERVER_PORT,${IPS[1]}:$CONFIG_SERVER_PORT,${IPS[2]}:$CONFIG_SERVER_PORT
EOF

# 用户mongodb授权
chown -R mongodb.mongodb $MONGODB_CLUSTER_HOME

}

function start_servers() {
  SERVER="$1"
  echo -e "==================== 启动 $SERVER.conf ====================\n"
  if [ "$SERVER" = "mongos" ]; then
    su - mongodb -c "$MONGODB_CLUSTER_HOME/mongodb-$VERSION/bin/mongos -f $CONF_HOME/$SERVER.conf"
  else
    su - mongodb -c "$MONGODB_CLUSTER_HOME/mongodb-$VERSION/bin/mongod -f $CONF_HOME/$SERVER.conf"
  fi
  wait_time
}

echo -e "==================== MongoDB Cluster 安装 ====================\n"

DIR=$(cd $(dirname $0) && pwd)
cd $DIR

source /etc/profile

if [ -f /etc/rc.d/init.d/mongodb ]; then
    rm -f /etc/rc.d/init.d/mongodb
fi
cp mongodb /etc/rc.d/init.d
chmod +x /etc/rc.d/init.d/mongodb
chkconfig --add mongodb
chkconfig mongodb on

init_environment
install_mongodb_cluster

start_servers config
start_servers shard_server1
start_servers shard_server2
start_servers shard_server3
start_servers mongos

echo -e "==================== 仅在第一个服务器节点执行auto_init_cluster.sh ====================\n"
